// pages/order/orderpay/orderpay.js
const orderApi = require("../../../api/order.js")
const balanceApi = require("../../../api/balance.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModel: false,
    id: 0,
    order: {},
    orders: [],
    total: 0,
    password: '',
    ids: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.id) {
      this.setData({
        id: options.id
      })
    } else if (options.ids) {
      let ids = []
      ids = options.ids.split(',')
      this.setData({
        ids: ids
      })
    }
  },
  // 根据id获取订单
  getOrderById() {
    let that = this
    if (this.data.id != 0) {
      orderApi.getOrderById(this.data.id).then(res => {
        console.log(res)
        that.setData({
          order: res.data.data[0],
          total: res.data.data[0].total
        })
      })
    } else if (this.data.ids.length > 0) {
      let ids = this.data.ids
      let orders = []
      let total = 0
      ids.forEach(value => {
        orderApi.getOrderById(value).then(res => {
          if (res.data.code == 0) {
            orders.push(res.data.data[0])
            total += parseInt(res.data.data[0].total)
            console.log(total)
            that.setData({
              total: total,
              orders: orders
            })
          }
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getOrderById()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 获取输入框密码
  onChangePassword(e) {
    this.setData({
      password: e.detail
    })
  },
  // 确定付款
  pay() {
    let password = ''
    if (this.data.ids.length > 0) {
      password = this.data.orders[0].common_user.pay_password
    } else if(this.data.id != 0){
      password = this.data.order.common_user.pay_password
    }
    if(!password) {
      wx.showToast({
        title: '当前还没设置支付密码',
        icon: 'none'
      })
      return
    }
    if(!this.data.password) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return
    }
    let total = 0
    let balance = 0
    let newBalance = 0
    let data = {}
    if (this.data.password == password) {
      if (this.data.orders.length > 0) {
        balance = this.data.orders[0].common_user.balance
        if(parseInt(balance) < parseInt(this.data.total)) {
          wx.showToast({
            title: '余额不足，前去充值或取消订单',
            icon: 'none',
            duration: 4000
          })
          return
        }
        newBalance = parseInt(balance) - parseInt(this.data.total)
      } else {
        total = this.data.order.total
        balance = this.data.order.common_user.balance
        if (parseInt(balance) < parseInt(this.data.total)) {
          wx.showToast({
            title: '余额不足，前去充值或取消订单',
            icon: 'none',
            duration: 4000
          })
          return
        }
        newBalance = parseInt(balance) - parseInt(total)
      }
      data.balance = newBalance
      let token = wx.getStorageSync('token')
      balanceApi.updateBalance(data, token).then(res => {
        if (res.data.code == 0) {
          wx.showToast({
            title: '支付成功',
            duration: 3000,
            success() {
              wx.switchTab({
                url: '../../personal/personal',
              })
              app.setNum()
            }
          })
        }
        let data2 = {
          status: 1
        }
        if (this.data.orders.length > 0) {
          this.data.ids.forEach(value => {
            orderApi.updateOrderStatus(value, data2).then(res => {
              console.log('订单状态改变')
            })
          })
        } else {
          orderApi.updateOrderStatus(this.data.id, data2).then(res => {
            console.log('订单状态有待付款变为待收货')
          })
        }
      })  
    } else {
      wx.showToast({
        title: '支付密码错误',
        icon: 'none'
      })
    }
  },
  // 清空输入框
  cancel() {
    this.setData({
      password: ''
    })
  }
})